package com.czl.module_storehouse.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.util.SparseArray;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.utils.GlideUtils;
import com.czl.module_storehouse.R;
import com.lihang.ShadowLayout;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xubin
 */
public class SelectGoodsAdapter extends BaseQuickAdapter<SortBean, BaseViewHolder> {


    private Map<String, SortBean> mSelectedMap = new LinkedHashMap<>();
    private Map<String, SortBean> mSelectedOldMap = new LinkedHashMap<>();

    private OnSelectedItemChangeListener mOnSelectedItemChangeListener;

    public SelectGoodsAdapter(int layoutResId, @Nullable List<SortBean> data) {
        super(layoutResId, data);
    }

    public Map<String, SortBean> getSelectedMap() {
        return mSelectedMap;
    }

    public void setSelectedMap(Map<String, SortBean> selectedMap) {
        mSelectedMap = selectedMap;
    }

    public Map<String, SortBean> getSelectedOldMap() {
        return mSelectedOldMap;
    }

    public void setSelectedOldMap(Map<String, SortBean> selectedOldMap) {
        mSelectedOldMap = selectedOldMap;
    }

    public List<SortBean> getSelectedList() {

        if (mSelectedMap == null) {
            return new ArrayList<>();
        }
        return new ArrayList<>(mSelectedMap.values());
    }

    public List<SortBean> getSelectedNewList() {
        List<SortBean> list = new ArrayList<>();
        if (mSelectedMap != null) {
            list.addAll(mSelectedMap.values());
            for (SortBean sortBean : mSelectedOldMap.values()) {
                list.remove(sortBean);
            }
        }
        return list;
    }

    public void setSelectedList(List<SortBean> selectedList) {
        if (selectedList != null) {
            for (SortBean sortBean : selectedList) {
                sortBean.setClickable(false);
                if (sortBean.getUuidSortString() == null || sortBean.getUuidSortString().isEmpty()) {
                    mSelectedMap.put(String.valueOf(sortBean.getSortId()), sortBean);
                    mSelectedOldMap.put(String.valueOf(sortBean.getSortId()), sortBean);
                } else {
                    mSelectedMap.put(sortBean.getUuidSortString(), sortBean);
                    mSelectedOldMap.put(sortBean.getUuidSortString(), sortBean);
                }
            }
        }
    }

    private void clickItem(CheckBox checkBox, SortBean item, int position) {
        item.setSelected(!item.selected());
        checkBox.setChecked(item.selected());
//        setViewData(position, "selected");
        if (item.selected()) {
//            mSelectedArray.put(item.getSortId(), item);
            mSelectedMap.put(String.valueOf(item.getSortId()), item);
        } else {
//            SortBean sortBean = getSortBean(item);
//            if (sortBean != null) {
//            }
//            mSelectedArray.remove(item.getSortId());
            mSelectedMap.remove(String.valueOf(item.getSortId()));
        }
        if (mOnSelectedItemChangeListener != null) {
            mOnSelectedItemChangeListener.onSelectedItemChange(position, item);
        }
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, SortBean item) {
        View view = helper.getView(R.id.cl_view);
        helper.setText(R.id.tv_goods_name, item.getSortName())
                .setText(R.id.tv_goods_code, "编号：" + item.getSortCode())
                .setText(R.id.tv_goods_norm, "型号：" + item.getSortModel());

        ImageView imageView = helper.getView(R.id.iv_goods);
        GlideUtils.loadImage(getContext(), imageView, item.getSortImg(), R.mipmap.pic_default_loading, R.mipmap.pic_default_error);
        CheckBox checkBox = helper.getView(R.id.check_box);
        checkBox.setChecked(item.selected());
        if (item.clickable()) {
            checkBox.setBackgroundResource(R.drawable.selector_check_circle);
            checkBox.setEnabled(true);
            helper.itemView.setEnabled(true);
            checkBox.setOnClickListener(v -> clickItem(checkBox, item, helper.getLayoutPosition()));
            helper.itemView.setOnClickListener(v -> clickItem(checkBox, item, helper.getLayoutPosition()));
        } else {
            helper.itemView.setEnabled(false);
            checkBox.setEnabled(false);
            checkBox.setBackgroundResource(R.drawable.ic_check_select_enable_18dp);
//            checkBox.setOnClickListener(null);
//            holder.itemView.setOnClickListener(null);
        }
        ShadowLayout shadowLayout = helper.getView(R.id.shadowLayout);
        shadowLayout.setShadowHiddenBottom(helper.getLayoutPosition() != getDefItemCount() - 1);
    }

    public void setOnSelectedItemChangeListener(OnSelectedItemChangeListener onSelectedItemChangeListener) {
        mOnSelectedItemChangeListener = onSelectedItemChangeListener;
    }

    public interface OnSelectedItemChangeListener {
        void onSelectedItemChange(int position, SortBean sortBean);
    }
}
